using System;
using System.Collections.Generic;
using System.Text;


namespace DataSchema.Settings
{
    static public class SettingsMgr
    {
        #region PRIVATE
        static private DataSchema.Settings.SettingSchema Sett;
        static private string SettingPath = "settings.xml";
        #endregion

        #region STATIC CONTRUCTOR
        static SettingsMgr()
        {
            Sett = new DataSchema.Settings.SettingSchema();
            if (new System.IO.FileInfo(SettingPath).Exists == false)
            {
                DataSchema.Settings.SettingSchema.DefaultRow r = Sett.Default.NewDefaultRow();
                r.CollectionPath = "c:\\";
                r.CSVPath = "c:\\";
                Sett.Default.Rows.Add(r);
                Sett.WriteXml(SettingPath);
            }
            else
                Sett.ReadXml(SettingPath);
        }
        #endregion

        #region DEFAULTS

        

        static public bool UseBackgroundOperations
        {
            get {
                if(((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).IsUseBackgroundOperationsNull())
                    return true;
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).UseBackgroundOperations;
            }
            set
            {
                ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).UseBackgroundOperations = value;
            }
        }


        static public bool UseCRC32Chache
        {
            get {
                if(((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).IsUseCRC32CacheNull())
                    return true;
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).UseCRC32Cache;
            }
            set
            {
                ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).UseCRC32Cache = value;
            }
        }


        static public string DefaultCollectionsPath
        {
            get {
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).CollectionPath;
            }
        }

        static public string DefaultCSVPath
        {
            get
            {
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).CSVPath;
            }
        }

        static public string DefaultCDDrive
        {
            get
            {
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).CDDrive;
            }
        }

        static public int LogPriority
        {
            get
            {
                if(((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).IsLogPriorityNull())
                    return 0;
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).LogPriority;
            }
        }

        static public bool MatchFirstByCRC
        {
            get
            {
                if (((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).IsMatchFirstByCRCNull())
                    return false;
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).MatchFirstByCRC;
            }
        }

        static public bool RemoveEmptyFolders
        {
            get
            {
                if(((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).IsRemoveEmptyFoldersNull())
                    return true;

                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).RemoveEmptyFolders;
            }
        }


        static public bool ShowCollectionExplorer
        {
            get
            {
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).ShowCollectionExlorer;
            }
        }

        static public bool ShowOutput
        {
            get
            {
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).ShowOutput;
            }
        }

        static public string WriteToOutput
        {
            get {
                if(((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).IsWriteToOutupNull())
                    return "";
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).WriteToOutup;
            }
        }

        static public bool NeededOnlyPictures
        {
            get
            {
                if (((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).IsNeededOnlyPicturesNull())
                    return true;
                return ((DataSchema.Settings.SettingSchema.DefaultRow)(Sett.Default.Rows[0])).NeededOnlyPictures;
            }
        }

        #endregion

        #region DATASET
        static public DataSchema.Settings.SettingSchema Settings
        {
            get
            {
                return Sett;
            }
        }

        static public DataSchema.Settings.SettingSchema.MyCollectionDataTable WatcherSettings
        {
            get
            {
                return Sett.MyCollection;
            }
        }

        static public DataSchema.Settings.SettingSchema.DefaultRow Defaults
        {
            get
            {
                return (DataSchema.Settings.SettingSchema.DefaultRow)Sett.Default.Rows[0];
            }
        }

        #endregion

        #region MYCOLLECTION

        static private string startuppath = null;

        static public string StartUpPath
        {
            get { return startuppath; }
            set { startuppath = value; }
        }
	
        static public void SaveMyCollection()
        {
            if(Sett.HasChanges())
                Sett.WriteXml(StartUpPath + SettingPath);
        }

        static public DataSchema.Settings.SettingSchema.SitesRow UpdateSite(string name)
        {
            System.Data.DataRow[] mcr = Sett.Sites.Select("Name = '" + name + "'");
            if (mcr == null || mcr.Length == 0)
            {
                DataSchema.Settings.SettingSchema.SitesRow nr = Sett.Sites.NewSitesRow();
                nr.Name = name;
                nr.Description = name;
                Sett.Sites.Rows.Add(nr);
                return nr;
            }
            return null;
        }


        static public DataSchema.Settings.SettingSchema.MyCollectionRow 
            UpdateMyCollection(string name, string site, string path)
        {

            System.Data.DataRow[] mcr = Sett.MyCollection.Select("Name = '" + name + "'");
            System.Data.DataRow[] mcs = Sett.Sites.Select("Name = '" + site + "'");
            if (mcr == null || mcr.Length == 0 && mcs != null && mcs.Length == 1)
            {
                DataSchema.Settings.SettingSchema.MyCollectionRow nr = Sett.MyCollection.NewMyCollectionRow();
                nr.Name = name;
                nr.SiteName = site;
                nr.Path = path + name + DataSchema.DataMgr.EXTDS;
                Sett.MyCollection.Rows.Add(nr);
                
                return nr;
            }
            return null;
        }

        static public bool RemoveFromMyCollection(string collname)
        {
            DataSchema.Settings.SettingSchema.MyCollectionRow[] nr = (DataSchema.Settings.SettingSchema.MyCollectionRow[])Sett.MyCollection.Select("Name = '" + collname + "'");
            if(nr.Length == 1)
            {
                Sett.MyCollection.Rows.Remove(nr[0]);
                return true;
            }
            return false;
        }

        static public DataSchema.Settings.SettingSchema.SitesRow NewSite(string n, string d)
        {
            DataSchema.Settings.SettingSchema.SitesRow sr = DataSchema.Settings.SettingsMgr.Settings.Sites.NewSitesRow();
            sr.Name = n;
            sr.Description = d;
            DataSchema.Settings.SettingsMgr.Settings.Sites.Rows.Add(sr);
            return sr;
        }

        static public bool DeleteSite(string n)
        {
            foreach(DataSchema.Settings.SettingSchema.SitesRow sr in Sett.Sites.Rows)
            {
                if(sr.Name == n)
                {
                    Sett.Sites.Rows.Remove(sr);
                    return true;
                }
            }
            return false;
        }

        static public DataSchema.Settings.SettingSchema.MyCollectionRow[] GetSiteCollections(DataSchema.Settings.SettingSchema.SitesRow sr)
        {

            return (DataSchema.Settings.SettingSchema.MyCollectionRow[])sr.GetChildRows("FK_Sites_MyCollection");
        }


        #endregion
    }
}
